package com.test.qnews.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.test.qnews.Love;
import com.test.qnews.LoveDao;
import com.test.qnews.R;
import com.test.qnews.Recommend;
import com.test.qnews.beans.NetDataBean;
import com.test.qnews.db.DaoSingleton;
import com.test.qnews.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class CommonAdapter extends BaseAdapter {
    private List<NetDataBean> data;
    private List<Recommend> recommends;
    private List<Love> loves;
    private Context context;
    final int Type_One = 1;
    final int Type_Two = 0;
    final int Type_Three = 2;


    /*
     收藏
     */
    public CommonAdapter(int i, List<Love> loves, Context context) {
        this.loves = loves;
        this.context = context;
        change();
    }

    //倒序将数据库中的数据set到data中,传到adapter中
    private void change() {
        data = new ArrayList<>();
        for (int i = 0; i < loves.size(); i++) {
            NetDataBean netDataBean = new NetDataBean();
            netDataBean.setItem_type(loves.get(loves.size()-i-1).getItem_type());
            netDataBean.setImageUrl(loves.get(loves.size()-i-1).getImageUrl());
            netDataBean.setTitle(loves.get(loves.size()-i-1).getTitle());
            netDataBean.setContent(loves.get(loves.size()-i-1).getContent());
            data.add(netDataBean);
        }
    }

    public CommonAdapter(Context context, List<Recommend> recommends) {
        this.context = context;
        this.recommends = recommends;
        Log.d("CommonAdapter", "recommends:" + recommends);
    }

    public CommonAdapter(List<NetDataBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return null == data ? recommends.size() : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.size() > 0 && null != data ? data.get(position) : recommends != null ? recommends.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        int i = null == data ? recommends.get(position).getItem_type() :
                data.get(position).getItem_type();
        if (i == 0) {
            return Type_Two;
        } else if (i == 1) {
            return Type_One;
        } else {
            return Type_Three;
        }
    }


    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 viewHolder1 = null;
        ViewHolder2 viewHolder2 = null;
        ViewHolder3 viewHolder3 = null;

        int type = getItemViewType(position);

        if (convertView == null) {
            switch (type) {
                case Type_One:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_text, null);
                    viewHolder1 = new ViewHolder1(convertView);
                    convertView.setTag(viewHolder1);
                    break;
                case Type_Two:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_text_image_left_r, null);
                    viewHolder2 = new ViewHolder2(convertView);
                    convertView.setTag(viewHolder2);
                    break;
                case Type_Three:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_text_image_top_b, null);
                    viewHolder3 = new ViewHolder3(convertView);
                    convertView.setTag(viewHolder3);
                    break;
            }
        } else {
            switch (type) {
                case Type_One:
                    viewHolder1 = (ViewHolder1) convertView.getTag();
                    break;
                case Type_Two:
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                    break;
                case Type_Three:
                    viewHolder3 = (ViewHolder3) convertView.getTag();
                    break;
            }
        }
        switch (type) {
            case Type_One:
                if (null == data) {
                    viewHolder1.titleTv1.setText(recommends.get(position).getTitle());
                } else {
                    viewHolder1.titleTv1.setText(data.get(position).getTitle());
                }
                break;
            case Type_Two:
                if (null == data) {
                    viewHolder2.titleTv2.setText(recommends.get(position).getTitle());
                    VolleySingleton.getInstance().loadImage(viewHolder2.leftIv2,
                            recommends.get(position).getImageUrl());
                } else {
                    viewHolder2.titleTv2.setText(data.get(position).getTitle());
                    VolleySingleton.getInstance().loadImage(viewHolder2.leftIv2,
                            data.get(position).getImageUrl());
                }
                break;
            case Type_Three:
                if (null == data) {
                    viewHolder3.titleTv3.setText(recommends.get(position).getTitle());
                    VolleySingleton.getInstance().loadImage(viewHolder3.topIv3,
                            recommends.get(position).getImageUrl());
                } else {
                    viewHolder3.titleTv3.setText(data.get(position).getTitle());
                    VolleySingleton.getInstance().loadImage(viewHolder3.topIv3,
                            data.get(position).getImageUrl());
                }
                break;

        }

        return convertView;
    }

    //只有内容
    class ViewHolder1 {
        TextView titleTv1;
        TextView timeTv1;

        public ViewHolder1(View view) {
            titleTv1 = (TextView) view.findViewById(R.id.title_tv_item);
            timeTv1 = (TextView) view.findViewById(R.id.time_tv_item);
        }
    }

    //图片在右面
    class ViewHolder2 {
        TextView titleTv2;
        TextView timeTv2;
        NetworkImageView leftIv2;

        public ViewHolder2(View view) {
            titleTv2 = (TextView) view.findViewById(R.id.title_tv_item_left_right);
            timeTv2 = (TextView) view.findViewById(R.id.time_tv_item_left_right);
            leftIv2 = (NetworkImageView) view.findViewById(R.id.picture_iv_item_left_right);
        }
    }

    //图片在上面
    class ViewHolder3 {
        TextView titleTv3;
        TextView timeTv3;
        NetworkImageView topIv3;

        public ViewHolder3(View view) {
            titleTv3 = (TextView) view.findViewById(R.id.title_tv_item_top_b);
            timeTv3 = (TextView) view.findViewById(R.id.time_tv_item_top_b);
            topIv3 = (NetworkImageView) view.findViewById(R.id.picture_iv_item_top_b);
        }
    }


}
